package com.ateam.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.ApplicationsBiz;
import com.ateam.pojo.Applications;
import com.ateam.pojo.Users;
import com.ateam.util.PojoFactory;

/**
 * @author WuYuan 请假、申请材料等申请
 */
@Controller
public class ApplicationsController {
	@Resource(name = "applicationsBiz")
	private ApplicationsBiz applicationsBiz;

	// 提交申请
	@ResponseBody
	@RequestMapping("submitapplication.do")
	public boolean submitapplication(String approverid, String description, HttpSession session,
			HttpServletRequest request) {

		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users applicant = (Users) session.getAttribute("currentUser");
				Applications application = PojoFactory.getApplication();
				application.setDescription(description);
				if (0 != applicationsBiz.addApplication(application, applicant.getUserid(),
						Integer.parseInt(approverid))) {
					return true;
				}
				return false;
			}
			return false;
		}
		return false;
	}

	// 审批者获得所有向他提交的审批
	@ResponseBody
	@RequestMapping("getallapplications.do")
	public List<Applications> getallapplications(HttpSession session, HttpServletRequest request) {
		// 后期修改代码，当前用户和公司都用工厂类获得
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users approver = (Users) session.getAttribute("currentUser");
				return applicationsBiz.getApplicationsByApprover(approver.getUserid());
			} else
				return null;
		} else
			return null;
	}

	// 申请者查看自己目前提交的申请状态
	@ResponseBody
	@RequestMapping("getapplicationsofapplicant.do")
	public List<Applications> getapplicationsofapplicant(HttpSession session, HttpServletRequest request) {
		// 后期修改代码，当前用户和公司都用工厂类获得

		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users applicant = (Users) session.getAttribute("currentUser");
				return applicationsBiz.getApplicationsByApplicant(applicant.getUserid());
			} else
				return null;
		} else
			return null;
	}

	// 审批者通过申请
	@ResponseBody
	@RequestMapping("throughapplication.do")
	public boolean throughapplication(String applicationid, HttpSession session, HttpServletRequest request) {

		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				if (0 != applicationsBiz.updateThroughApplication(Integer.parseInt(applicationid))) {
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}
}
